package troopmanager.mooresr.us;

import java.util.TreeSet;

import javax.swing.AbstractListModel;

public class SortedListModel<E> extends AbstractListModel {

	private static final long serialVersionUID = 1L;
	private TreeSet<E> model;

	public SortedListModel() {
		super();
		this.model = new TreeSet<E>();;
	}

	@SuppressWarnings("unchecked")
	@Override
	public E getElementAt(int arg0) {
		
		return  (E) model.toArray()[arg0];
	}

	@Override
	public int getSize() {
		
		return model.size();
	}
	
	public int size() {
		return getSize();
	}

	public Object get(int index) {
		return getElementAt(index);
	
	}
	
	public void addElement(E o) {
	    if (model.add(o)) {
	        fireContentsChanged(this, 0, getSize());
	    }		
	}
	
	public boolean removeElement(E o) {
	    boolean removed = model.remove(o);
	    if (removed) {
	      fireContentsChanged(this, 0, getSize());
	    }
	    return removed;   
		
	}
	
	public Object remove(int index) {
		E o = (E) getElementAt(index);
		removeElement(o);
		return o;
	}
	
	public void clear() {
		model.clear();
		fireContentsChanged(this, 0, getSize());
	}

}
